//package com.example.shardingwythe.config;
//
///**
// * @author 二师兄
// * @date 2021/9/7  2:07 下午
// */
//
//import cn.hutool.core.date.DateUtil;
//import com.google.common.collect.Range;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
//import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
//import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
//import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
//import org.springframework.stereotype.Component;
//
//import java.util.Collection;
//import java.util.Date;
//import java.util.LinkedHashSet;
//
///**
// * <h1>范围值控制分库分表</h1>
// *
// * @author gengzi
// * @date 2020年7月2日11:07:15
// */
//@Slf4j
//@Component
//public class InMonthDataBaseRangeShardingConfiguration implements PreciseShardingAlgorithm<Date>, RangeShardingAlgorithm<Date> {
//    /**
//     * Sharding.
//     *
//     * @param availableTargetNames available data sources or tables's names
//     * @param shardingValue        sharding value
//     * @return sharding results for data sources or tables's names
//     */
//    @Override
//    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Date> shardingValue) {
//        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
//        Range<Date> range = shardingValue.getValueRange();
//
//        Date lowerDate = range.lowerEndpoint();
//        Date upperDate = range.upperEndpoint();
//
//        log.info("lowerDate : {} ", DateUtil.formatDateTime(lowerDate));
//        log.info("upperDate : {} ", DateUtil.formatDateTime(upperDate));
//
//        Integer low = Integer.valueOf(DateUtil.format(lowerDate, "MM"));
//        Integer upper = Integer.valueOf(DateUtil.format(upperDate, "MM"));
//
//
//        for (int i = low; i <= upper; i++) {
//            for (String each : availableTargetNames) {
//                if (each.endsWith(i + "")) {
//                    result.add(each);
//                }
//            }
//        }
//        return result;
//    }
//
//    /**
//     * Sharding.
//     *
//     * @param availableTargetNames available data sources or tables's names
//     * @param shardingValue        sharding value
//     * @return sharding result for data source or table's name
//     */
//    @Override
//    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Date> shardingValue) {
//        // shardingValue 分片的值 日期，2020-06-06 15:12:00  根据年份分表，获取年份最小的，扩展年份最大的
//        // 假如只扩展两年的 2020  2021。
//        //配置的分片的sharding-column对应的值
//        Date timeValue = shardingValue.getValue();
//        String time = DateUtil.formatDate(timeValue);
//        log.info("time: {}", time);
//        //分库时配置的sharding-column
//        String timeColumn = shardingValue.getColumnName();
//        //需要分库的逻辑表
//        String tableName = shardingValue.getLogicTableName();
//        log.info("分表策略： 分片值 : {} , 分片列: {} , 逻辑表: {} ", time, timeColumn, tableName);
//        availableTargetNames.forEach(name -> {
//            log.info("availableTargetName ：{} ", name);
//        });
//        //按年路由
//        for (String each : availableTargetNames) {
//            String value = time.substring(5, 7);
//            if (each.endsWith(Integer.parseInt(value) + "")) {
//                return each;
//            }
//        }
//        throw new UnsupportedOperationException();
//    }
//}